package com.seal.commons.query.jdbctemplate;

import com.seal.commons.query.DefaultQueryAdapter;
import com.seal.commons.query.DefaultQueryResult;
import com.seal.commons.query.QueryRequest;
import com.seal.commons.query.QueryResult;
import com.seal.commons.query.utils.FilterTextManipulator;
import com.seal.commons.query.utils.PageSqlSupport;
import com.seal.commons.query.utils.TextManipulator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import java.util.LinkedList;
import java.util.List;

/**
 * 功能描述: <br/>
 *
 * @author yong.huang
 * @date: 2020-06-28 21:27<br/>
 * @since JDK 1.8
 */
@Slf4j
public class PagingJbcQueryAdapter extends DefaultQueryAdapter {

    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private List<TextManipulator> manipulators;

    private String databaseType;

    @Override
    public <T> QueryResult<T> query(String name, QueryRequest request, Class<T> clazz) {
        String sql = getSql();
        request.setPage();
        StringBuffer execuleSql = PageSqlSupport.getQuery(sql, databaseType);
        for(TextManipulator textManipulator : getManipulators()){
            execuleSql = textManipulator.manipulate(execuleSql, request.getFilters());
        }
        if(isShowSql()){
            log.info("query sql:[{}]", execuleSql);
        }
        List<T> list = namedParameterJdbcTemplate.queryForList(execuleSql.toString(), request.getFilters(),  clazz);
        StringBuffer countSql = PageSqlSupport.getCountQuery(sql, databaseType);
        for(TextManipulator textManipulator : getManipulators()){
            countSql = textManipulator.manipulate(countSql, request.getFilters());
        }
        if(isShowSql()){
            log.info("query count sql:[{}]", execuleSql);
        }
        SqlRowSet sqlRowSet = namedParameterJdbcTemplate.queryForRowSet(countSql.toString(), request.getFilters());
        if(sqlRowSet.next()){
            int count = sqlRowSet.getInt(1);
            request.setTotalNumber(count);
        }
        return new DefaultQueryResult<>(list, request);

    }




    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

    public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }

    public List<TextManipulator> getManipulators() {
        if(manipulators == null){
            manipulators = new LinkedList<>();
            manipulators.add(new FilterTextManipulator());
        }
        return manipulators;
    }

    public void setManipulators(List<TextManipulator> manipulators) {
        this.manipulators = manipulators;
    }

    public String getDatabaseType() {
        return databaseType;
    }

    public void setDatabaseType(String databaseType) {
        this.databaseType = databaseType;
    }
}